<?php
if(!defined('MY_ROOT'))
{throw new Exception('Access Denied');}
class jwxt{
	public static function getkcb($user){
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_URL,'http://jwxt.shmtu.edu.cn/shmtu/courseTableForStd.action');
	curl_setopt($ch,CURLOPT_AUTOREFERER,true);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($ch,CURLOPT_COOKIEFILE,'cookie_'.$user.'.txt');
	curl_setopt($ch,CURLOPT_COOKIEJAR,'cookie_'.$user.'.txt');
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($ch,CURLOPT_POST,false);
	$content=curl_exec($ch);
	$d=stripos($content,'"ids"');
	$c= substr($content,$d+7,20);
	//echo $c."\n";
	preg_match('/^(\d+)\"/',$c,$m);
	$ids=$m[1];
	//echo $ids;
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_URL,'http://jwxt.shmtu.edu.cn/shmtu/courseTableForStd!courseTable.action');
	curl_setopt($ch,CURLOPT_AUTOREFERER,true);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
	curl_setopt($ch,CURLOPT_POST,true);
	$data=array('ignoreHead'=>1,'setting.kind'=>'std','startWeek'=>1,'semester.id'=>33,'ids'=>$ids);
	curl_setopt($ch,CURLOPT_COOKIEFILE,'cookie_'.$user.'.txt');
	curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	$content=curl_exec($ch);
	$start=stripos($content,'var fakeCourses = [];');
	$end=stripos($content,'function containFakeCourse(fakeCourse)');
	$content=trim(substr($content,$start+21,$end-$start-22));
	$content=str_replace("table0.activities[index][table0.activities[index].length]=activity;",'',$content);
	$tmparr=explode("activity =",$content);
	foreach( $tmparr as $cura){
		$cur=explode(";",$cura);
		foreach($cur as $les){
			$cus[]=trim($les);
		}
	}
	$scf=false;
	unset($tmparr);
	$table=array(array());
	for ($i=0;$i<count($cus);$i++){
	if($scf==true){
		$scf=false;
		$tmp=explode(",",substr($cus[$i],17,-1));
		$tmp=str_replace('"',"",$tmp);
		$cut=true;
		while(1){
			if($cus[$i+1]==""||($i+1)>=count($cus)){
			$cut=true;
			break;
			}
			else{
				$i++;
				sscanf($cus[$i],"index =%d*unitCount+%d",$date,$num);
				$date=(string) $date; $num=(string)$num;
				if($cut){
				$loc=strrpos($tmp[3],'(');
				$tmp[3]=substr($tmp[3],0,$loc);
				$cut=false;
				}
        		$table[$date][$num][]=array('teacher'=>$tmp[1],'name'=>$tmp[3],'place'=>$tmp[5],'week'=>str_split($tmp[6],1));
				}	
		}
	
	}
	if($cus[$i]==""){$scf=true;}
	}
	return $table;
	}
	public static function login($user,$pass){
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_URL,'http://jwxt.shmtu.edu.cn/shmtu/login.action');
	curl_setopt($ch,CURLOPT_POST,true);
	curl_setopt($ch,CURLOPT_AUTOREFERER,true);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
	$data=array('username'=>$user,'password'=>$pass,'submitBtn'=>'submit');
	curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
	curl_setopt($ch,CURLOPT_COOKIEJAR,'cookie_'.$user.'.txt');
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	$d=curl_exec($ch);
	curl_close($ch);
	return preg_match('/'.$user.'/',$d);
	}
	public static function getscore($user){
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,'http://jwxt.shmtu.edu.cn/shmtu/teach/grade/course/person!search.action?semesterId=3&projectType=');
		curl_setopt($ch,CURLOPT_AUTOREFERER,true);
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
		curl_setopt($ch,CURLOPT_COOKIEFILE,'cookie_'.$user.'.txt');
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		$content=curl_exec($ch);
		$st=stripos($content,'</thead>');
		$end=stripos($content,'</tbody>');
		$content=substr($content,$st+9,$end-$st);
		$xml=simplexml_load_string($content);
		$i=0;
		foreach ( $xml->tr as $tr){
		$sc[$i]['name']=trim($tr->td[3]);
		$sc[$i]['score']=trim($tr->td[9]);
		$sc[$i]['gp']=trim($tr->td[10]);
		$i++;
		}
		return $sc;
	}
}
?>